﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using WatiN.Core;
using WebAutomation.Browsers;

namespace WebAutomation
{
    public class WaitUntilCommand : CommandBase
    {
        public WaitUntilCommand(WebBrowserObject browserObject)
        {
            this.Initialize(browserObject);

        }
        public override bool ExecuteCommand(WebBrowserObject browserObject, string[] parameter, string url, string testRepo, string testResultLocation, string dbConnString, ref DataSet dataFromDB, ref TestLineResult testLineResult, ref bool stopTest, WebAutoUtils.ITestDataExceptionRules testDataExceptionRules)
        {
            Element Button = null;
            Div div = null;
            string command = parameter[0];
            testLineResult.LineCommand = command;
            testLineResult.LineParameter = parameter[1];
            if (this.BrowserObject == null)
            {
                this.BrowserObject = browserObject;
            }
            try
            {

                switch (command.ToLower())
                {
                    case "divvisible":

                        if (IsDivVisible(parameter[1]))
                        {
                            WaitUntilDivIsVisible(parameter[1]);
                            testLineResult.LinePassed = true;
                        }
                        else
                        {
                            testLineResult.LineMessage = "Could not find Div:" + parameter[1];
                            testLineResult.LinePassed = false;
                        }
                        break;

                    case "buttonvalueexists":
                        WaitUntilButtonByValueExists(parameter[1]);
                        testLineResult.LinePassed = true;
                        break;
                    case "buttonidexists":
                        WaitUntilButtonByIDExists(parameter[1]);
                        testLineResult.LinePassed = true;
                        break;
                    case "buttonnameexists":
                        WaitUntilButtonByNameExists(parameter[1]);
                        testLineResult.LinePassed = true;
                        break;

                    case "time":
                        int time = Convert.ToInt32(parameter[1].Replace("WaitUntil", ""));
                        WaitUntilTimeExpires(time);
                        testLineResult.LinePassed = true;
                        break;
                    case "pageloads":
                        WaitUntilPageLoads();
                        testLineResult.LinePassed = true;
                        break;
                }
            }
            catch (Exception ex)
            {
                testLineResult.IsFrameworkError = true;
                testLineResult.LineMessage = "Error." + ex.Message;
                testLineResult.LinePassed = false;
            }
            return testLineResult.LinePassed;
        }

        public virtual bool IsDivVisible(string divID)
        {
            bool returnValue = false;
            object div = null;
            div = this.BrowserObject.Div(Find.ById(divID));
            bool isDivFound = false;
            if (div == null)
            {
                div = this.BrowserObject.Div(Find.ByName(divID));
            }
            else
            {
                div = this.BrowserObject.Div(Find.ByValue(divID));
                if (div != null)
                {
                    isDivFound = true;
                }
            }

            return returnValue;
        }

        public virtual void WaitUntilDivIsVisible(string divID)
        {
            this.BrowserObject.DivWaitUntilAttributeValue(divID,"display", "inline");
        }

        public virtual void WaitUntilButtonByValueExists(string buttonValue)
        {
            BrowserObject.ButtonWaitUntilExists(Find.ByValue(buttonValue));
        }

        public virtual void WaitUntilButtonByIDExists(string buttonId)
        {
            BrowserObject.ButtonWaitUntilExists(Find.ById(buttonId));
        }

        public virtual void WaitUntilButtonByNameExists(string buttonName)
        {
            BrowserObject.ButtonWaitUntilExists(Find.ByName(buttonName));
        }

        public virtual void WaitUntilTimeExpires(int milliSecondsToWait)
        {
            Thread.Sleep(milliSecondsToWait);
        }

        public virtual void WaitUntilPageLoads()
        {
            BrowserObject.WaitForComplete();
        }
    }
}
